package classinteger;

import java.math.BigDecimal;
import java.math.BigInteger;

public class BigDecimoTest {

    public static void main(String[] args) {
        //BingDecimal是解决浮点数精度丢失的问题
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.2");
        //相加
        System.out.println(bd1.add(bd2));
        //相减
        System.out.println(bd1.subtract(bd2));
        //相乘
        System.out.println(bd1.multiply(bd2));
        //相除
        System.out.println("相除："+bd1.divide(bd2));
        //相除的保留位数并且选择取舍方式的方法
        //2位  bd1除以bd2，除不尽的时候，采用这个三个参数指定的取舍方式，
        //默认是四舍五入，0表示向上取整，1表示向下取整，4表示四舍五入，5表示五舍六入
        BigDecimal b1 = new BigDecimal("0.01");
        BigDecimal b2 = new BigDecimal("0.03");
        BigDecimal bd7 = b1.divide(b2, 2, 0);
        System.out.println("相除："+bd7);//0.01
    }
}
class BigIntegerTest {
    public static void main(String[] args) {
        // long num = 1000000000000000000000000000000000000000L;
        // String
        String num2 = "1000000000000000000000000000000000000000";
        String num3 = "2";
        // 创建BigInteger的对象
        BigInteger big1 = new BigInteger("1000000000000000000000000000000000000000");
        System.out.println("big1 = " + big1.toString());// big1 = 1000000000000000000000000000000000000000
        // 绝对值的方法
        BigInteger abs = big1.abs();
        System.out.println("abs = " + abs);//1000000000000000000000000000000000000000
        BigInteger big2 = new BigInteger("3");
        // 加法运算
        BigInteger add = big1.add(big2);
        System.out.println("add = " + add);//add = 1000000000000000000000000000000000000002
        // 减法运算
        BigInteger subtract = big1.subtract(big2);
        System.out.println("subtract = " + subtract);//subtract = 999999999999999999999999999999999999998
        // 乘法运算
        BigInteger multiply = big1.multiply(big2);
        System.out.println("multiply = " + multiply);//multiply = 2000000000000000000000000000000000000000
        // 除法运算
        BigInteger divide = big1.divide(big2);
        System.out.println("divide = " + divide);//divide = 333333333333333333333333333333333333333
        // 取余运算
        BigInteger re = big1.remainder(big2);
        System.out.println("re = " + re);
        // 除法运算并返回商和余数
        BigInteger[] bigIntegers = big1.divideAndRemainder(big2);
        System.out.println(bigIntegers[0]);
        System.out.println(bigIntegers[1]);
        System.out.println(bigIntegers.length);
    }
 }
